//
//  MainViewController.swift
//  WeiBo
//
//  Created by 史亚洲 on 16/1/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //MARK:给按钮添加监听事件
    @objc private func plusBtnClick(){
    
    print(__FUNCTION__)
    
    }
    

    
    override func viewDidLoad() {
        super.viewDidLoad()
        print(tabBar.classForCoder)
        // Do any additional setup after loading the view.
        
        
        //必须的属性
        let mainTabBar = MainTabBar()
        //系统的tabBar是只读属性,不能直接修改,可以使用KVC间接修改
        setValue(mainTabBar, forKey: "tabBar")
        mainTabBar.plusButton .addTarget(self, action: "plusBtnClick", forControlEvents: .TouchUpInside)
     
        print(tabBar.classForCoder)

        
        //根控制器入口
        addChildViewControllers()
    }
    
    
    //扩充调用函数,添加其他控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName:"tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    
        //添加子视图控制器,重构代码,抽取参数,private私有的,不希望外界修改
    private func addChildViewController(vc: UIViewController,title: String,imageName: String){
        let nav = UINavigationController(rootViewController: vc)
        
        //标题
        vc.title = title
        
        //修改图片显示样式
        self.tabBar.tintColor = UIColor.orangeColor()
        
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(nav)

        
    }
    

}
